<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="商品名称" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入商品名称" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="序号" align="center" type="index" width="55">
        <template #default="{ $index }">
          {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="商品图片" width="100" align="center" prop="image">
        <template #default="scope">
          <el-image style="width: 50px; height: 50px;" :src="scope.row.image" :preview-src-list="[scope.row.image]"
            preview-teleported>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="投放形式" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button v-if="scope.row.isPublish" link type="primary" icon="edit"
            @click="handleUpdate(scope.row)">投放形式</el-button>
        </template>
      </el-table-column>


      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="状态" align="center" prop="isPublish">
        <template #default="scope">
          <span v-if="scope.row.isPublish == true">已发布</span>
          <span v-if="scope.row.isPublish == false">未发布</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人员" align="center" prop="updateUserName" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" v-if="!scope.row.isPublish"
            @click="handlePublish(scope.row)">发布</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
      v-model:limit="queryParams.pageSize" @pagination="getList" />




    <!-- 投放形式 -->
    <el-dialog :title="titleName" v-model="open" width="780px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="80px">
        <el-row>

          <!-- 普通 -->
          <template v-if="data.queryParams.type == 3">
            <template v-if="form.isSort == 0">
              <el-col :span="12">
                <el-form-item label="投放范围" prop="scope">
                  <el-select v-model="form.scope" placeholder="">
                    <el-option v-for="item in scopeList" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="广告类型" prop="categoryList">
                  <el-input value="普通广告" placeholder="请输入广告类型" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="列表位置" prop="positionList">
                  <el-input v-model="form.positionList" placeholder="请输入列表位置" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="投放时间" prop="timeList">
                  <el-input v-model="form.timeList" placeholder="请输入时间" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="品类位置" prop="categoryList">
                  <el-input v-model="form.categoryList" placeholder="请输入品类位置" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="投放日期" prop="dateList">
                  <el-input v-model="form.dateList" placeholder="请输入日期" />
                </el-form-item>
              </el-col>
            </template>
            <template v-if="form.isSort == 1">
              <el-col :span="12">
                <el-form-item label="投放范围" prop="scope">
                  <el-select v-model="form.scope" placeholder="">
                    <el-option v-for="item in scopeList" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="广告类型" prop="categoryList">
                  <el-input value="竞价排名" placeholder="请输入广告类型" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="列表位置" prop="positionList">
                  <el-input v-model="form.positionList" placeholder="请输入列表位置" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="开始日期" prop="startDate">
                  <el-input v-model="form.startDate" placeholder="请输入开始日期" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="品类位置" prop="categoryList">
                  <el-input v-model="form.categoryList" placeholder="请输入品类位置" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="结束日期" prop="endDate">
                  <el-input v-model="form.endDate" placeholder="请输入结束日期" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="位置排名" prop="sort">
                  <el-input v-model="form.sort" placeholder="请输入位置排名" />
                </el-form-item>
              </el-col>
            </template>
          </template>
          <!-- 新品 -->
          <template v-if="data.queryParams.type == 1">
            <el-col :span="12">
              <el-form-item label="投放范围" prop="scope">
                <el-select v-model="form.scope" placeholder="">
                  <el-option v-for="item in scopeList" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="投放时间" prop="timeList">
                <el-input v-model="form.timeList" placeholder="请输入时间" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="列表位置" prop="positionList">
                <el-input v-model="form.positionList" placeholder="请输入列表位置" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="投放日期" prop="dateList">
                <el-input v-model="form.dateList" placeholder="请输入日期" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="品类位置" prop="dateList">
                <el-input value="新品发布" placeholder="请输入品类位置" />
              </el-form-item>
            </el-col>
          </template>
          <!-- 特价 -->
          <template v-if="data.queryParams.type == 2">
            <el-col :span="12">
              <el-form-item label="投放范围" prop="scope">
                <el-select v-model="form.scope" placeholder="">
                  <el-option v-for="item in scopeList" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="投放时间" prop="timeList">
                <el-input v-model="form.timeList" placeholder="请输入时间" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="列表位置" prop="positionList">
                <el-input v-model="form.positionList" placeholder="请输入列表位置" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="投放日期" prop="dateList">
                <el-input v-model="form.dateList" placeholder="请输入日期" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="品类位置" prop="dateList">
                <el-input value="特价商品" placeholder="请输入品类位置" />
              </el-form-item>
            </el-col>

          </template>


        </el-row>
      </el-form>
    </el-dialog>

    <!-- 发布 -->
    <el-dialog :title="title" v-model="open2" width="780px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <!-- 品类位置 -->
          <el-col :span="12">
            <el-form-item label="品类位置" prop="systemProductCategoryIdList">
              <el-select v-model="form.systemProductCategoryIdList" placeholder="请选择位置" multiple>
                <el-option v-for="dict in systemProductCategoryIdList" :key="dict.value" :label="dict.name"
                  :value="dict.systemProductCategoryId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 位置 -->
          <el-col :span="12">
            <el-form-item label="广告位置" prop="positionList">
              <el-select v-model="form.positionList" placeholder="请选择位置" multiple>
                <el-option v-for="dict in arr3" :key="dict.value" :label="dict.title" :value="dict.type"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 范围 -->
          <el-col :span="12">
            <el-form-item label="广告范围" prop="scope">
              <el-select v-model="form.scope" placeholder="请选择范围">
                <el-option v-for="dict in scopeList" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 排名 -->
          <el-col :span="12">
            <el-form-item label="广告发布排名" prop="scope">
              <el-select v-model="form.sort" placeholder="请选择排名">
                <el-option v-for="dict in sortList" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 日期 -->
          <el-col :span="12">
            <el-form-item label="开始日期" prop="startDate">
              <el-date-picker class="flex-1" v-model="form.startDate" value-format="YYYY-MM-DD" type="date"
                range-separator="To" start-placeholder="Start date" end-placeholder="End date" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期" prop="endDate">
              <el-date-picker class="flex-1" v-model="form.endDate" value-format="YYYY-MM-DD" type="date"
                range-separator="To" start-placeholder="Start date" end-placeholder="End date" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格" prop="spec">
              <el-input v-model="form.spec" placeholder="请输入规格" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="型号" prop="model">
              <el-input v-model="form.model" placeholder="请输入型号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="价格" prop="price">
              <el-input v-model="form.price" type="number" placeholder="请输入价格" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产厂家" prop="produceMerchantName">
              <el-input v-model="form.produceMerchantName" placeholder="请输入生产厂家" />
            </el-form-item>
          </el-col>


        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>




  </div>
</template>

<script setup name="Notice">

import {
  ruleScope,
  rulePosition
} from "@/api/backAdvertisementRule/backAdvertisementRule.js";



let systemProductCategoryIdList = ref([])
let sortList = [
  { label: "第一位", value: 1 },
  { label: "第二位", value: 2 },
  { label: "第三位", value: 3 },
  { label: "第四位", value: 4 },
]

let arr3 = [
  { title: "用户端云平台界面医疗机构列表", type: 1, checked: false },
  { title: "用户端护工抢单界面商品列表", type: 2, checked: false },
  { title: "用户端状态查询界面商品列表", type: 3, checked: false },
  { title: "用户端我的界面商品列表", type: 4, checked: false },
  { title: "管理端医疗机构界面商品列表", type: 5, checked: false },

  { title: "管理端业务拓展界面商品列表", type: 6, checked: false },

  { title: "管理端日常经营界面商品列表", type: 7, checked: false },

  { title: "管理端生产物流界面商品列表", type: 8, checked: false },
  { title: "管理端我的信息界面商品列表", type: 9, checked: false },
];

let scopeList = [
  { label: "所在地一公里内发布", value: 1 },
  { label: "所在区县内发布", value: 2 },
  { label: "所在市内发布", value: 3 },
  { label: "所在省内发布", value: 4 },
  { label: "所在全国内发布", value: 5 },
]

import {
  advertisementProductApiPublish,
  listNotice,
  delNotice,
} from "@/api/backAdvertisementProduct/backAdvertisementProduct";

import {
  categoryListNormal,
  advertisementRuleApiRuleSort,
  categoryListSpecial,
} from "@/api/backAdvertisementRule/backAdvertisementRule";

import {
  advertisementProductApisupervisedetail
} from "@/api/backAdvertisementProduct/backAdvertisementProduct";


import {
  advertisementRuleApirule
} from "@/api/backAdvertisementRule/backAdvertisementRule.js";



import { watch } from "vue";

const route = useRoute()
const routeType = ref(1)

const { proxy } = getCurrentInstance();
const { scope_status } = proxy.useDict(
  "scope_status"
);

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    currentPage: 1,
    pageSize: 10,
    productName: undefined,
    type: 1,
  },
  rules: {
  },
});
const { queryParams, form, rules } = toRefs(data);




const titleName = ref(1)
// type 1新品 2特价 3普通
if (route.meta.title == '广告商品') {
  data.queryParams.type = 3
  titleName.value = '广告商品投放详情'
} else if (route.meta.title == '新品发布') {
  data.queryParams.type = 1
  titleName.value = '新品发布投放详情'
} else if (route.meta.title == '特价商品') {
  data.queryParams.type = 2
  titleName.value = '特价商品投放详情'
}

watch(route, () => {
  console.log(route.meta.title, "route.meta.title");
  if (route.meta.title == '广告商品') {
    routeType.value = 3
  } else if (route.meta.title == '新品发布') {
    routeType.value = 1
  } else if (route.meta.title == '特价商品') {
    routeType.value = 2
  }
}, { immediate: true })



/** 查询商品列表 */
function getList() {
  loading.value = true;
  console.log(routeType.value, 'routeType.value');
  queryParams.value.type = routeType.value
  console.log(queryParams.value.type, 'routeType.value');
  listNotice(queryParams.value).then((response) => {
    noticeList.value = response.records;
    total.value = response.total;
    loading.value = false;
  });
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}



/** 表单重置 */
function reset() {
  form.value = {
  }
  proxy.resetForm("noticeRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.currentPage = 1;
  getList();
}
/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const advertisementProductId = row.advertisementProductId || ids.value;
  advertisementProductApisupervisedetail(advertisementProductId).then((response) => {
    form.value = response;
    open.value = true;
    title.value = "投放形式";
  });
}

let open2 = ref(false)
/* 发布操作 */
function handlePublish(row) {
  reset();
  open2.value = true;
  title.value = "发布广告图片商品";
  form.value.advertisementProductId = row.advertisementProductId
  let scopeType = 0
  if (routeType.value == 1) {
    scopeType = 3
  } else if (routeType.value == 2) {
    scopeType = 3
  } else if (routeType.value == 3) {
    scopeType = 4
  }
  // 获取设置过的值
  advertisementRuleApirule(scopeType).then((re) => {
    if (re.sortList) {
      sortList = re.sortList.map(val => sortList.find(item => item.value === val));
    }
    scopeList = re.scopeList.map((val) => scopeList.find(i => val == i.value))
    arr3 = re.positionList.flatMap((g) => {
      return arr3.find((i) => i.type === g) || null;
    }).filter(Boolean);
    systemProductCategoryIdList.value = re.categoryObjectList
  })
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      advertisementProductApiPublish(form.value).then((response) => {
        proxy.$modal.msgSuccess("修改成功");
        open2.value = false;
        getList();
      });
    }
  });
}
/** 取消按钮 */
function cancel() {
  open2.value = false;
  reset();
}

// TODO
/** 删除按钮操作 */
function handleDelete(row) {
  const advertisementProductIds = row.advertisementProductId || ids.value;
  proxy.$modal
    .confirm('是否确认删除商品编号为"' + advertisementProductIds + '"的数据项？')
    .then(function () {
      return delNotice(advertisementProductIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("成功");
    })
    .catch(() => { });
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.advertisementProductId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}




getList();

</script>